package com.igame.Service.impl;

import com.igame.Service.CollectService;
import com.igame.Service.GoodsService;
import com.igame.common.Result;
import com.igame.constant.GoodsConstant;
import com.igame.entity.Collect;
import com.igame.entity.Goods;
import com.igame.mapper.CollectMapper;
import com.igame.mapper.GoodsMapper;
import com.igame.vo.GoodsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.List;

@Service
public class CollectImpl implements CollectService {

    @Autowired
    CollectMapper collectMapper;

    @Autowired
    GoodsMapper goodsMapper;
    @Override
    @Transactional
    public Result addCollect(Integer userId, Integer goodsId) {
        try {
            Collect collect = collectMapper.selectCollect(userId, goodsId);
            if (collect==null){
                int i = collectMapper.InsertCollect(userId,goodsId);
                if (i==0){
                    throw new RuntimeException("收藏失败请稍后再试");
                }
                return Result.success();
            }
            return  Result.error("");
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
           return Result.error(e.getMessage());
        }
    }

    @Override
    @Transactional
    public Result deleteCollect(Integer userId, Integer goodsId) {
        try {
            Collect collect = collectMapper.selectCollect(userId, goodsId);
            if(collect!=null){
                int i = collectMapper.deleteCollect(userId,goodsId);
                if (i==0){
                    throw new RuntimeException("收藏更新失败请稍后再试");
                }
                return Result.success();
            }
         return Result.error("");
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.error(e.getMessage());
        }
    }

    @Override
    public Result selectCollect(Integer userId, Integer goodsId) {
        try {
            Collect collect = collectMapper.selectCollect(userId, goodsId);
            if(collect!=null){
                return Result.success();
            }
            return Result.error("");
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @Override
    public Result<List<GoodsVo>> getMyCollection(Integer id) {
        List<Integer> ids = collectMapper.selectByUserId(id);
        List<GoodsVo> goodsVoList =new ArrayList<>();

        for (Integer integer : ids) {
            GoodsVo goodsVo =new GoodsVo();
            Goods goodsById = goodsMapper.getGoodsById(integer);
            BeanUtils.copyProperties(goodsById,goodsVo);
            if(goodsById.getState()==0){
                goodsVo.setState(GoodsConstant.onSale);
            }else {
                goodsVo.setState(GoodsConstant.sole);
            }
            goodsVoList.add(goodsVo);
        }

        return Result.success(goodsVoList);
    }
}
